package com.fs.controller.general;


import com.fs.controller.PicFitUtil;
import com.fs.controller.vo.PageData;
import com.fs.controller.vo.price.ReqGetPrice;
import com.fs.controller.vo.price.RespRoomPrice;
import com.fs.controller.vo.room.ReqRoomQuery;
import com.fs.controller.vo.room.RespRoom;
import com.fs.controller.vo.shop.RespGetPics;
import com.fs.global.config.CustomConfig;
import com.fs.global.init.RoleName;
import com.fs.service.PriceService;
import com.fs.service.RoomService;
import com.fs.service.bo.BoPageInfo;
import com.fs.service.bo.price.BoPriceWithRoom;
import com.fs.service.bo.price.BoQueryParam;
import com.fs.service.bo.room.BoRoom;
import com.fs.service.bo.room.BoRoomQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.File;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Api(tags = "房间API(共用)", description = "管理员和小程序用户共用")
@RestController
@RequestMapping(path = {"/v2/room", "/v1/room"})
@RequiresRoles(value = {RoleName.GUEST, RoleName.CUSTOMER, RoleName.ADMIN}, logical = Logical.OR)
@Validated
public class RoomPublicController {

    private final RoomService roomService;
    private final PriceService priceService;
    private final PicFitUtil picFitUtil;
    private final CustomConfig config;

    public RoomPublicController(RoomService roomService, PriceService priceService, PicFitUtil picFitUtil, CustomConfig config) {
        this.roomService = roomService;
        this.priceService = priceService;
        this.picFitUtil = picFitUtil;
        this.config = config;
    }

    @ApiOperation("获取房间信息")
    @GetMapping
    public PageData<RespRoom> queryRoom(ReqRoomQuery req) {
        BoRoomQueryParam param = new BoRoomQueryParam().setName(req.getRoomName()).setRoomId(req.getRoomId());
        BoPageInfo<BoRoom> pageInfo = roomService.query(param, req.getPageNo(), req.getPageSize());

        List<RespRoom> roomList = pageInfo.getResults().stream().map(room -> {

            String imageDir = picFitUtil.getAbsImageDirOfRoom(room.getId());
            File dir = new File(imageDir);
            if (!dir.exists()) {
                return new RespRoom(room, "");
            }
            String[] pics = dir.list();
            if (pics == null || pics.length == 0) {
                return new RespRoom(room, "");
            }
            String relPath = picFitUtil.getRelImagePathOfRoom(pics[0], room.getId());
            String picUrl = String.format("%s/display?path=%s&op=thumbnail&w=100&h=100", config.getPicfitAddress(), relPath);
            return new RespRoom(room, picUrl);

        }).collect(Collectors.toList());

        return new PageData<>(roomList, pageInfo.getTotal());
    }

    @ApiOperation("获取房间照片")
    @GetMapping("/pics")
    public RespGetPics getRoomPics(@RequestParam Integer roomId) {
        String fileSaveDir = picFitUtil.getAbsImageDirOfRoom(roomId);

        File dir = new File(fileSaveDir);
        String[] files = dir.list();
        if (files == null) {
            return new RespGetPics().setBaseUrl(config.getPicfitAddress());
        }

        List<String> pics = Stream.of(files).map(
                picName -> String.join(File.separator, Arrays.asList(picFitUtil.getRoomSavePicDir(), roomId.toString(), picName))
        ).collect(Collectors.toList());
        return new RespGetPics().setBaseUrl(config.getPicfitAddress()).setPics(pics);
    }

    @ApiOperation("查询房价价格")
    @GetMapping("/getPrice")
    PageData<RespRoomPrice> getAllPrice(@Valid ReqGetPrice req) {
        LocalTime beginDateTime = req.getBeginDateTime() == null ? null : LocalTime.parse(req.getBeginDateTime());
        LocalTime endDateTime = req.getEndDateTime() == null ? null : LocalTime.parse(req.getEndDateTime());
        BoQueryParam param = BoQueryParam.builder().
                roomId(req.getRoomId()).
                price(req.getPrice()).
                endDateTime(endDateTime).
                beginDateTime(beginDateTime).build();
        BoPageInfo<BoPriceWithRoom> result = priceService.query(param, req.getPageNo(), req.getPageSize());
        List<RespRoomPrice> data = result.getResults().stream().map(RespRoomPrice::new).collect(Collectors.toList());
        return new PageData<>(data, result.getTotal());
    }
}
